//index.js
var app = getApp();
//引入这个插件，使html内容自动转换成wxml内容
var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    good:{},//商品
    cartGoodCount: 0,
    proid: 0,
    indicatorDots: true,
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 800,
    imgUrls: [],
    Hei: "", //轮播图容器高度

    goods_id:"", //ID
    goods_name: "", //名称
    goods_integral:'',//积分
    goods_price: "", //价格
    goods_info: "", //简介
    goods_banner: [], //轮播图片（数组）

    sessionkey: '',
    goods_paytype: '1', //支付类型(默认微信支付)
  },

  imgH: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth;         //获取当前屏幕的宽度
    var imgh = e.detail.height;　　　　　　　　　　　　　　　　//图片高度
    var imgw = e.detail.width;
    var swiperH = winWid * imgh / imgw - 5 + "px"　　　　　　　　//等比设置swiper的高度。    ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    this.setData({
      Hei: swiperH　　　　　　　　//设置高度
    })
  },

  // 获取商品详情
  onLoad: function (option) {
    var that = this;
    that.setData({
      proid: option.id,
    });
    that.loadDetail();
    that.upDateCount();
  },
  onShow: function () {
    app.authinfo();
  },

  loadDetail: function () {
    var that = this;
    wx.request({
      url: app.d.hostUrl + '/api/shop/getShopDetail',
      method: 'post',
      data: {
        id: that.data.proid
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res);
        // --init data 

        var shop_detail = res.data.data.content;

        WxParse.wxParse('shop_detail', 'html', shop_detail, that, 0);

        that.setData({
          shop: res.data.data,

        });
      },
      error: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon:"none",
          duration: 2000,
        });
      },

    });
  },


  // 拨打电话
  opentel: function () {
    app.gotel();
  },

  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function () {
    var openid = wx.getStorageSync('openid');
    from: menu;
    return {
      title: '走吧商城',
      path: '/pages/onepage/onepage?openid=' + openid,
      success: function (res) {
        // console.log(openid);      
      },
      fail: function (res) {
        // console.log(res);
      }
    }
  },
/**
 * 添加到购物车
 */
addToCart: function () {
  let flag = true;
    let num = 0;

    app.globalData.selectGoods.forEach((item, index) => {
      // 通过唯一的kid判断购物车是否已经存在商品
      if (this.data.goods_id == item.id) {
        flag = false;
      }
    });
    
    if (flag) {
      app.globalData.selectGoods.push(this.data.goods);
      this.upDateCount();
      wx.showToast({
        title: '加入购物车成功',
        duration: 2000,
      });
      console.log(app.globalData.selectGoods);
    } else {
      wx.showToast({
        title: '已在购物车',
        icon:"none",
        duration: 1000
      });
    }
},
upDateCount() {
  let count = app.globalData.selectGoods.length;
  // app.globalData.selectGoods.forEach((item, index) => {
  //   count += item.count;
  // });
  console.log(count);
  this.setData({
    cartGoodCount: count
  })
}
})
